﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Net;

namespace RedisSamples
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        public static void Warn(string txt)
        {
            MessageBox.Show(txt, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        public static void Info(string txt)
        {
            MessageBox.Show(txt, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void BindClusterTree()
        {
            treeAllKeys.Nodes.Clear();
            var conn = RedisHelper.GetConn();
            ICollection<ClusterNode> nodes=null;
            foreach (var point in conn.GetEndPoints())
            {
                nodes = conn.GetServer(point).ClusterConfiguration.Nodes;
                break;
            }
            if (nodes == null) { return; }
            //Master
            foreach (ClusterNode node in nodes)
            {
                if (!node.IsSlave)
                {
                    //Master
                    TreeNode masterServer = new TreeNode(node.EndPoint.ToString());
                    masterServer.ImageIndex = 2;
                    masterServer.SelectedImageIndex = 2;
                    masterServer.Tag = new TreeNodeArg() { DBIndex = 0 };
                    treeAllKeys.Nodes.Add(masterServer);
                    //Master的Keys
                    IEnumerable<RedisKey> allKeys = conn.GetServer(node.EndPoint).Keys(0, "*");
                    //keys节点
                    foreach (var item in allKeys)
                    {
                        TreeNode tn = new TreeNode(item.ToString());
                        tn.ImageIndex = 1;
                        tn.SelectedImageIndex = 1;
                        tn.Tag = new TreeNodeArg() { DBIndex = 0, KeyText = item.ToString() };
                        masterServer.Nodes.Add(tn);
                    }

                    foreach (ClusterNode node2 in nodes)
                    {
                        if (node2.IsSlave && node.EndPoint.ToString()==node2.Parent.EndPoint.ToString())
                        {
                            //Slave
                            TreeNode slaveServer = new TreeNode(node2.EndPoint.ToString());
                            slaveServer.ImageIndex = 3;
                            slaveServer.SelectedImageIndex = 3;
                            slaveServer.Tag = new TreeNodeArg() { DBIndex = 0 };
                            masterServer.Nodes.Add(slaveServer);
                            //Slave的Keys
                            //Master的Keys
                            IEnumerable<RedisKey> allKeys2 = conn.GetServer(node.EndPoint).Keys(0, "*");
                            //keys节点
                            foreach (var item in allKeys2)
                            {
                                TreeNode tn = new TreeNode(item.ToString());
                                tn.ImageIndex = 1;
                                tn.SelectedImageIndex = 1;
                                tn.Tag = new TreeNodeArg() { DBIndex = 0, KeyText = item.ToString() };
                                slaveServer.Nodes.Add(tn);
                            }
                        }
                    }
                }
            }
            treeAllKeys.ExpandAll();
        }
        private void BindOrignalTree()
        {
            treeAllKeys.Nodes.Clear();
            var conn = RedisHelper.GetConn();
            //遍历所有服务器
            foreach (var point in conn.GetEndPoints())
            {

                for (int i = 0; i < 16; i++)
                {
                    IServer server = conn.GetServer(point);
                    IEnumerable<RedisKey> allKeys = server.Keys(i, "*");
                    //数据库节点
                    TreeNode tnDB = new TreeNode();
                    tnDB.Tag = new TreeNodeArg() { DBIndex = i };
                    tnDB.ImageIndex = 0;
                    tnDB.SelectedImageIndex = 0;
                    //keys节点
                    int count = 0;
                    foreach (var item in allKeys)
                    {
                        //key
                        TreeNode tn = new TreeNode(item.ToString());
                        tn.ImageIndex = 1;
                        tn.SelectedImageIndex = 1;
                        tn.Tag = new TreeNodeArg() { DBIndex = i, KeyText = item.ToString() };
                        tnDB.Nodes.Add(tn);
                        count++;
                    }
                    tnDB.Text = i.ToString() + "(" + count + ")";
                    treeAllKeys.Nodes.Add(tnDB);
                }
            }
            treeAllKeys.ExpandAll();
        }
        /// <summary>
        /// 绑定所有的Key
        /// </summary>
        private void BindTree()
        {
            var conn1 = RedisHelper.GetConn();
            IServer server = conn1.GetServer(conn1.GetEndPoints().First<EndPoint>());
            //集群模式
            if (server.ServerType == ServerType.Cluster)
            {
                BindClusterTree();
            }
            else
            {
                BindOrignalTree();
            }
            



        }
        /// <summary>
        /// 表单加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                StackExchange.Redis.ConnectionMultiplexer.Connect(ConfigurationManager.AppSettings["RedisConnStr"] ?? "127.0.0.1:7000");
            }
            catch (Exception ex)
            {
                Warn(ex.Message);
                this.Close();
            }
            BindTree();
        }
        private void BeforeFill()
        {
            txtStrKey.Text = string.Empty;
            txtStrValue.Text = string.Empty;
            txtHashKey.Text = string.Empty;
            txtHashValues.Text = string.Empty;
            txtListKey.Text = string.Empty;
            txtListValues.Text = string.Empty;
            txtSetKey.Text = string.Empty;
            txtSetValues.Text = string.Empty;
        }
        /// <summary>
        /// 填充键值
        /// </summary>
        /// <param name="keyType"></param>
        /// <param name="arg"></param>
        private void Fill(RedisType keyType, TreeNodeArg arg)
        {
            switch (keyType.ToString())
            {
                case "String":
                    {
                        txtStrKey.Text = arg.KeyText;
                        txtStrValue.Text = RedisHelper.StringGet(arg.DBIndex,arg.KeyText);
                        break;
                    }
                case "Hash":
                    {
                        txtHashKey.Text = arg.KeyText;
                        foreach(HashEntry entry in RedisHelper.HashGet(arg.DBIndex, arg.KeyText))
                        {
                            txtHashValues.AppendText(entry.Name+" "+entry.Value+"\n");
                        }
                        
                        break;
                    }
                case "List":
                    {
                        txtListKey.Text = arg.KeyText;
                        foreach(RedisValue val in RedisHelper.GetList(arg.DBIndex, arg.KeyText))
                        {
                            txtListValues.AppendText(val.ToString() + " ");
                        }
                        break;
                    }
                case "Set":
                    {
                        txtSetKey.Text = arg.KeyText;
                        foreach (RedisValue val in RedisHelper.GetSet(arg.DBIndex, arg.KeyText))
                        {
                            txtSetValues.AppendText(val.ToString() + " ");
                        }
                        break;
                    }
            }
        }
        private int GetSelectedDBIndex()
        {
            if (treeAllKeys.SelectedNode != null)
            {
                TreeNodeArg arg = (TreeNodeArg)treeAllKeys.SelectedNode.Tag;
                return arg.DBIndex;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 树形Key选择后事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeAllKeys_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeAllKeys.SelectedNode != null) { 
                TreeNodeArg arg = (TreeNodeArg)treeAllKeys.SelectedNode.Tag;
                //填充前操作
                BeforeFill();
                if (!string.IsNullOrEmpty(arg.KeyText))
                {
                    RedisType keyType = RedisHelper.GetRedisType(arg.DBIndex, arg.KeyText);
                    //填充
                    Fill(keyType, arg);
                }
            }
        }
        //保存Hash的值
        private void button1_Click_1(object sender, EventArgs e)
        {
            int dbIndex = GetSelectedDBIndex();
            //1 先删除
            RedisHelper.KeyDel(dbIndex, txtHashKey.Text.Trim());
            //2 后插入
            string key = txtHashKey.Text.Trim();
            foreach (string line in txtHashValues.Text.Trim().Split('\n'))
            {
                if(line.Split(' ').Length != 2)
                {
                    Warn("Hash的不同项之间用换行分隔，属性和值请用空格分隔");
                    return;
                }
                string itemKey = line.Split(' ')[0];
                string itemValue = line.Split(' ')[1];
                //设置值
                RedisHelper.HashSet(dbIndex, key, itemKey, itemValue);
            }
            BindTree();
        }
        //删除Hash的值
        private void button2_Click(object sender, EventArgs e)
        {
            int dbIndex = GetSelectedDBIndex();
            RedisHelper.KeyDel(dbIndex, txtHashKey.Text.Trim());
            BindTree();
        }
        //保存String
        private void button1_Click(object sender, EventArgs e)
        {
            try
            { 
                int dbIndex = GetSelectedDBIndex();
                RedisHelper.StringSet(dbIndex, txtStrKey.Text.Trim(), txtStrValue.Text.Trim());
                BindTree();
            }
            catch (Exception ex)
            {
                Warn(ex.Message);
            }
        }
        //删除String
        private void btnStrDel_Click(object sender, EventArgs e)
        {
            int dbIndex = GetSelectedDBIndex();
            RedisHelper.KeyDel(dbIndex, txtStrKey.Text.Trim());
            BindTree();
        }
        //保存List的值
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int dbIndex = GetSelectedDBIndex();
                RedisHelper.SetList(dbIndex, txtListKey.Text.Trim(), txtListValues.Text.Trim());
                BindTree();
            }
            catch (Exception ex)
            {
                Warn(ex.Message);
            }
        }
        //删除List的值
        private void button4_Click(object sender, EventArgs e)
        {
            int dbIndex = GetSelectedDBIndex();
            RedisHelper.KeyDel(dbIndex, txtListKey.Text.Trim());
            BindTree();
        }
        //保存Set的值
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                int dbIndex = GetSelectedDBIndex();
                foreach(string v in txtSetValues.Text.Trim().Split(' '))
                { 
                    RedisHelper.AddSet(dbIndex, txtSetKey.Text.Trim(),v);
                }
                BindTree();
            }
            catch (Exception ex)
            {
                Warn(ex.Message);
            }
        }
        //删除Set的值
        private void button6_Click(object sender, EventArgs e)
        {
            int dbIndex = GetSelectedDBIndex();
            RedisHelper.KeyDel(dbIndex, txtSetKey.Text.Trim());
            BindTree();
        }
        //并集
        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                int dbIndex = GetSelectedDBIndex();
                txtSetValues.Text = "";
                foreach (RedisValue val in RedisHelper.UnionSet(dbIndex, txtSetKey.Text.Trim(),SetOperation.Union))
                {
                    txtSetValues.AppendText(val.ToString() + " ");
                }
                BindTree();
            }
            catch (Exception ex)
            {
                Warn(ex.Message);
            }
        }
        //交集
        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                int dbIndex = GetSelectedDBIndex();
                txtSetValues.Text = "";
                foreach (RedisValue val in RedisHelper.UnionSet(dbIndex, txtSetKey.Text.Trim(), SetOperation.Intersect))
                {
                    txtSetValues.AppendText(val.ToString() + " ");
                }
                BindTree();
            }
            catch (Exception ex)
            {
                Warn(ex.Message);
            }
        }
        //差异
        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                int dbIndex = GetSelectedDBIndex();
                txtSetValues.Text = "";
                foreach (RedisValue val in RedisHelper.UnionSet(dbIndex, txtSetKey.Text.Trim(), SetOperation.Difference))
                {
                    txtSetValues.AppendText(val.ToString() + " ");
                }
                BindTree();
            }
            catch (Exception ex)
            {
                Warn(ex.Message);
            }
        }
        //写入文件
        private void button10_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(fileKey.Text.Trim()))
            {
                Warn("请录入Key！");
                return;
            }
            if (dlgFile.ShowDialog()==DialogResult.OK)
            {
                if (!File.Exists(dlgFile.FileName))
                {
                    Warn("文件不存在！");
                    return;
                }
                
                //写文件
                try
                {
                    Stopwatch w = new Stopwatch();
                    w.Start();
                    RedisHelper.FileInsert(GetSelectedDBIndex(), fileKey.Text.Trim(), dlgFile.FileName);
                    w.Stop();
                    Info("操作完成，共花费:" + w.ElapsedMilliseconds.ToString("#,##0.00")+"毫秒");
                    BindTree();
                }
                catch (Exception ex)
                {
                    Warn(ex.Message);
                }
            }
        }
        //读取文件
        private void button11_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(fileKey.Text.Trim()))
            {
                Warn("请录入Key！");
                return;
            }
            if (dlgSaveFile.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Stopwatch w = new Stopwatch();
                    w.Start();
                    RedisHelper.FileSaveAs(GetSelectedDBIndex(), fileKey.Text.Trim(), dlgSaveFile.FileName);
                    w.Stop();
                    Info("操作完成，共花费:" + w.ElapsedMilliseconds.ToString("#,##0") + "毫秒");
                    BindTree();
                }
                catch(Exception ex)
                {
                    Warn(ex.Message);
                }
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            RedisHelper.KeyDel(GetSelectedDBIndex(), fileKey.Text.Trim());
            BindTree();
        }
        //批量写入文件
        Dictionary<string, string> dictFile = new Dictionary<string, string>();
        private void button13_Click(object sender, EventArgs e)
        {
            dictFile = new Dictionary<string, string>();
            //1 产生测试数据
            for (var i = 0; i < numFileNum.Value; i++)
            {
                string key = BuildRandomKey();
                if (!dictFile.Keys.Contains<string>(key))
                {
                    dictFile.Add(key, BuildRandomFile());
                }
            }
            //2 写入
            Stopwatch watch = new Stopwatch();
            watch.Start();
            foreach(KeyValuePair<string,string> item in dictFile)
            { 
                RedisHelper.FileInsert(GetSelectedDBIndex(), item.Key, item.Value);
            }
            watch.Stop();
            //清除临时文件
            ClearTempFiles();
            //告知结果
            ShowResultInfo(watch, dictFile.Count);
        }
        private void ShowResultInfo(Stopwatch watch,int count)
        {
            if (watch.ElapsedMilliseconds == 0)
            {
                Info("操作完成，共花费" + watch.ElapsedMilliseconds.ToString("#,##0") + "毫秒，平均每个" + watch.ElapsedMilliseconds / count + "毫秒");
            }
            else
            {
                Info("操作完成，共花费" + watch.ElapsedMilliseconds.ToString("#,##0") + "毫秒，平均每个" + watch.ElapsedMilliseconds / count + "毫秒，一秒钟可以处理" + 1000 * count / watch.ElapsedMilliseconds + "个");
            }
            
        }
        //批量读取文件
        private void button14_Click(object sender, EventArgs e)
        {
            if (dictFile.Count == 0)
            {
                Warn("之前没有写入记录，暂时无法读取！");
                return;
            }
            Stopwatch watch = new Stopwatch();
            watch.Start();
            foreach (KeyValuePair<string,string> item in dictFile)
            {
                RedisHelper.FileSaveAs(GetSelectedDBIndex(), item.Key, item.Value);
            }
            watch.Stop();
            //清除临时文件
            ClearTempFiles();
            //告知结果
            ShowResultInfo(watch, dictFile.Count);
        }
        //字符串写入
        Dictionary<string, string> dictString = new Dictionary<string, string>();
        private void button16_Click(object sender, EventArgs e)
        {
            dictString = new Dictionary<string, string>();
            //1 准备好字符串
            for (int i = 0; i < numStrNum.Value; i++)
            {
                dictString.Add(BuildRandomKey(), BuildRandomValue());
            }
            //2 操作
            Stopwatch watch = new Stopwatch();
            watch.Start();
            foreach (KeyValuePair<string, string> item in dictString)
            {
                RedisHelper.StringSet(GetSelectedDBIndex(), item.Key, item.Value);
            }
            watch.Stop();
            //告知结果
            ShowResultInfo(watch, dictString.Count);
            
        }
        //字符串读取
        private void button15_Click(object sender, EventArgs e)
        {
            if (dictString.Count == 0)
            {
                Warn("之前没有写入记录，暂时无法读取！");
                return;
            }
            Stopwatch watch = new Stopwatch();
            watch.Start();
            foreach (KeyValuePair<string, string> item in dictString)
            {
                RedisHelper.StringGet(GetSelectedDBIndex(), item.Key);
            }
            watch.Stop();
            //告知结果
            ShowResultInfo(watch, dictString.Count);
        }

        private void ClearTempFiles()
        {
            foreach (string path in dictFile.Values)
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
             }
        }
        //生产一个大小范围内的随机文件
        private string BuildRandomFile()
        {
            //1 文件名
            string path = Path.Combine(Environment.CurrentDirectory, "TempFiles");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string fileName = Path.Combine(path, Guid.NewGuid().ToString() + ".tmp");
            //2 写入一些内容
            int fileSize = GetRangeRandom((int)numFileSizeMin.Value, (int)numFileSizeMax.Value);
            BinaryWriter bw = new BinaryWriter(File.OpenWrite(fileName));
            byte[] b = new byte[fileSize * 1024];
            bw.Write(b);
            bw.Flush();
            bw.Close();
            //3 返回文件的路径
            return fileName;
        }
        private int GetRangeRandom(int minLen, int maxLen)
        {
            //随机长度
            Random r = new Random((int)DateTime.Now.Ticks);
            int keyLen = r.Next(maxLen + 1);
            while (keyLen < minLen)
            {
                keyLen = r.Next(maxLen + 1);
            }
            return keyLen;
        }
        //生成一定范围内的随机长度的Key
        private string BuildRandomKey()
        {
            return BuildRandomString((int)numStrKeySizeMin.Value, (int)numStrKeySizeMax.Value);
        }
        //生成一定范围内的随机长度的Key
        private string BuildRandomValue()
        {
            return BuildRandomString((int)numValueSizeMin.Value, (int)numValueSizeMax.Value);
        }
        private string BuildRandomString(int minLen,int MaxLen)
        {

            int keyLen = GetRangeRandom((int)numStrKeySizeMin.Value, (int)numStrKeySizeMax.Value);
            //随机的key
            StringBuilder sbKey = new StringBuilder();
            int loop = (keyLen % 36 == 0 ? keyLen / 36 : (keyLen / 36) + 1);
            for (int i = 0; i < loop; i++)
            {
                sbKey.Append(Guid.NewGuid().ToString());
            }
            return sbKey.ToString().Substring(0, keyLen);
        }
        //清除所有的key
        private void button17_Click(object sender, EventArgs e)
        {
            RedisHelper.ClearAllKeys();
            dictFile = new Dictionary<string, string>();
            dictString = new Dictionary<string, string>();
            dictInt = new Dictionary<string, int>();
            dictFloat = new Dictionary<string, float>();
            dictByte = new Dictionary<string, byte>();
            dictChar = new Dictionary<string, char>();
            dictDecimal = new Dictionary<string, decimal>();
            dictBoolean = new Dictionary<string, bool>();
            //重新绑定key列表
            BindTree();
            Info("清理完毕！");
        }
        //简单数据类型测试-写入
        Dictionary<string, int> dictInt = new Dictionary<string, int>();
        Dictionary<string, float> dictFloat = new Dictionary<string, float>();
        Dictionary<string, byte> dictByte = new Dictionary<string, byte>();
        Dictionary<string, char> dictChar = new Dictionary<string, char>();
        Dictionary<string, decimal> dictDecimal = new Dictionary<string, decimal>();
        Dictionary<string, bool> dictBoolean = new Dictionary<string, bool>();

        private void button19_Click(object sender, EventArgs e)
        {
            if (cbxType.SelectedItem == null)
            {
                Warn("请先选择一个数据类型！");
                return;
            }
            dictInt = new Dictionary<string, int>();
            dictFloat = new Dictionary<string, float>();
            dictByte = new Dictionary<string, byte>();
            dictChar = new Dictionary<string, char>();
            dictDecimal = new Dictionary<string, decimal>();
            dictBoolean = new Dictionary<string, bool>();

            int dbIndex = GetSelectedDBIndex();
            Random r = new Random(DateTime.Now.Millisecond);
            switch (cbxType.SelectedItem.ToString())
            {
                case "Int32":
                    {
                        for (int i = 0; i < numTestNum.Value; i++)
                        {
                            dictInt.Add(BuildRandomKey(), r.Next(int.MaxValue));
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, int> item in dictInt)
                        {
                            RedisHelper.Set<int>(dbIndex, item.Key, item.Value);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictInt.Count);
                        break;
                    }
                case "Float":
                    {
                        for (int i = 0; i < numTestNum.Value; i++)
                        {
                            dictFloat.Add(BuildRandomKey(), (float)(r.Next(int.MaxValue) / 99.0));
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, float> item in dictFloat)
                        {
                            RedisHelper.Set<float>(dbIndex, item.Key, item.Value);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictFloat.Count);
                        break;
                    }
                case "Byte":
                    {
                        for (int i = 0; i < numTestNum.Value; i++)
                        {
                            dictByte.Add(BuildRandomKey(), (Byte)r.Next(256));
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, Byte> item in dictByte)
                        {
                            RedisHelper.Set<Byte>(dbIndex, item.Key, item.Value);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictByte.Count);
                        break;
                    }
                case "Char":
                    {
                        for (int i = 0; i < numTestNum.Value; i++)
                        {
                            dictChar.Add(BuildRandomKey(), Convert.ToChar(r.Next(255)));
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, Char> item in dictChar)
                        {
                            RedisHelper.Set<Char>(dbIndex, item.Key, item.Value);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictChar.Count);
                        break;
                    }
                case "Decimal":
                    {
                        for (int i = 0; i < numTestNum.Value; i++)
                        {
                            dictDecimal.Add(BuildRandomKey(), Convert.ToDecimal(r.Next(int.MaxValue) / 99.0));
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, Decimal> item in dictDecimal)
                        {
                            RedisHelper.Set<Decimal>(dbIndex, item.Key, item.Value);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictDecimal.Count);
                        break;
                    }
                case "Boolean":
                    {
                        for (int i = 0; i < numTestNum.Value; i++)
                        {
                            dictBoolean.Add(BuildRandomKey(), (r.Next(int.MaxValue) % 2) == 0 + 1);
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, Boolean> item in dictBoolean)
                        {
                            RedisHelper.Set<Boolean>(dbIndex, item.Key, item.Value);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictBoolean.Count);
                        break;
                    }
            }
        }
        //简单数据类型测试-读取
        private void button18_Click(object sender, EventArgs e)
        {
            if (cbxType.SelectedItem == null)
            {
                Warn("请先选择一个数据类型！");
                return;
            }
            int dbIndex = GetSelectedDBIndex();
            switch (cbxType.SelectedItem.ToString())
            {
                case "Int32":
                    {
                        if (dictInt.Count == 0)
                        {
                            Warn("之前没有写入，暂时无法读取！");
                            return;
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, int> item in dictInt)
                        {
                            RedisHelper.Get<Int32>(dbIndex, item.Key);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictInt.Count);
                        break;
                    }
                case "Float":
                    {
                        if (dictFloat.Count == 0)
                        {
                            Warn("之前没有写入，暂时无法读取！");
                            return;
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, float> item in dictFloat)
                        {
                            RedisHelper.Get<double>(dbIndex, item.Key);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictFloat.Count);
                        break;
                    }
                case "Byte":
                    {
                        if (dictByte.Count == 0)
                        {
                            Warn("之前没有写入，暂时无法读取！");
                            return;
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, Byte> item in dictByte)
                        {
                            RedisHelper.Get<Byte>(dbIndex, item.Key);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictByte.Count);
                        break;
                    }
                case "Char":
                    {
                        if (dictChar.Count == 0)
                        {
                            Warn("之前没有写入，暂时无法读取！");
                            return;
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, Char> item in dictChar)
                        {
                            RedisHelper.Get<Char>(dbIndex, item.Key);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictChar.Count);
                        break;
                    }
                case "Decimal":
                    {
                        if (dictDecimal.Count == 0)
                        {
                            Warn("之前没有写入，暂时无法读取！");
                            return;
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, Decimal> item in dictDecimal)
                        {
                            RedisHelper.Get<Decimal>(dbIndex, item.Key);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictDecimal.Count);
                        break;
                    }
                case "Boolean":
                    {
                        if (dictBoolean.Count == 0)
                        {
                            Warn("之前没有写入，暂时无法读取！");
                            return;
                        }
                        //测试
                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        foreach (KeyValuePair<string, Boolean> item in dictBoolean)
                        {
                            RedisHelper.Get<Boolean>(dbIndex, item.Key);
                        }
                        watch.Stop();
                        //告知结果
                        ShowResultInfo(watch, dictBoolean.Count);
                        break;
                    }
            }
        }
        //刷新左侧key树
        private void button20_Click(object sender, EventArgs e)
        {
            BindTree();

        }
    }
    /// <summary>
    /// 树形节点的附加对象
    /// </summary>
    public class TreeNodeArg
    {
        /// <summary>
        /// 节点绑定的Key,为空则表示当前结点为数据库
        /// </summary>
        public string KeyText { set; get; }
        /// <summary>
        /// 节点对应的数据库索引
        /// </summary>
        public int DBIndex { set; get; }
    }
}
